import time
import pygame

# 游戏初始化
pygame.init()

# 窗口的宽度和高度
WIDTH = 360
HEIGHT = 480

# 获得屏幕对象
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# 设置窗口标题
pygame.display.set_caption("植物大战僵尸")

# 游戏运行状态
running = True

# Colors (R, G, B)
BLACK = (0, 0, 0)
WHITE = (255, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 创建字体对象
myfont = pygame.font.Font(None, 60)

# 创建文本图像
# textImage = myfont.render("pygame", True, WHITE)

# 计数器
count = 0
# 开始时间
start = time.time()

# 帧率
FPS = 50
# 创建clock对象
clock = pygame.time.Clock()

# 游戏运行循环
while running:

    # 设置帧率
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    count+=1

    now = time.time()

    # 计算帧率
    fps = count/(now-start)
    fpsImage = myfont.render(str(fps), True, WHITE)

    # 双缓存，先在内存中绘制
    screen.fill(BLACK)

    # 显示文本
    screen.blit(fpsImage, (100, 100))

    pygame.display.flip()
